package com.example.dllo.keepdemo.dynamic;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.example.dllo.keepdemo.R;
import com.example.dllo.keepdemo.dynamic.bean.ExquisiteDynamicBean;
import com.example.dllo.keepdemo.person.PersonMessageActivity;
import com.example.dllo.keepdemo.tools.RoundImageView;
import com.example.dllo.keepdemo.tools.VolleySingleton;

/**
 * Created by dllo on 16/1/15.
 */
public class ExquisiteListViewAdapter extends BaseAdapter  {
    private Context context;
    private ExquisiteDynamicBean data;

    public ExquisiteListViewAdapter(Context context,ExquisiteDynamicBean data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return 10;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        MyViewHolder myViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_listview_activity_exquisitedynamic,parent,false);
            myViewHolder = new MyViewHolder(convertView);
            convertView.setTag(myViewHolder);
        } else {
            myViewHolder = (MyViewHolder) convertView.getTag();
        }
        myViewHolder.imgHeader.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, PersonMessageActivity.class);
                intent.putExtra("id", data.getData().get(position).getAuthor().get_id());
                context.startActivity(intent);
            }
        });
        myViewHolder.tvUserName.setText(data.getData().get(position).getAuthor().getUsername());
        myViewHolder.tvContent.setText(data.getData().get(position).getContent());
        myViewHolder.tvPraise.setText(data.getData().get(position).getLikes() + "");
        myViewHolder.tvComment.setText(data.getData().get(position).getComments() + "");

        ImageLoader.ImageListener listener1 = ImageLoader.getImageListener(myViewHolder.img,R.mipmap.planmain320_370,R.mipmap.no_photo);
        VolleySingleton.getImageLoader().get(data.getData().get(position).getPhoto(),listener1,360,350);


        if (data.getData().get(position).getAuthor().getAvatar().equals("")) {
            myViewHolder.imgHeader.setImageResource(R.mipmap.person_45_45);
        } else {
            ImageLoader.ImageListener listener = ImageLoader.getImageListener(myViewHolder.imgHeader,R.mipmap.person_45_45,R.mipmap.person_45_45);
            VolleySingleton.getImageLoader().get(data.getData().get(position).getAuthor().getAvatar(),listener);
        }

        return convertView;
    }


    class MyViewHolder {
        private RoundImageView imgHeader;
        private ImageView img;
        private TextView tvUserName,tvContent,tvPraise,tvComment;
        public MyViewHolder(View view) {
            imgHeader = (RoundImageView) view.findViewById(R.id.img_item_activity_exquisite_head);
            img = (ImageView) view.findViewById(R.id.img_item_activity_exquisite_img);
            tvUserName = (TextView) view.findViewById(R.id.tv_item_activity_exquisite_username);
            tvContent = (TextView) view.findViewById(R.id.tv_item_activity_exquisite_content);
            tvPraise = (TextView) view.findViewById(R.id.tv_item_activity_exquisite_praisenum);
            tvComment = (TextView) view.findViewById(R.id.tv_item_activity_exquisite_replynum);
        }
    }
}
